/*
 * File: BasicGraphTest.java 
 *  
 * Created on 20.12.2010
 */
package de.graph.common;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import de.graph.common.impl.GraphFactory;
import de.graph.common.impl.GraphFactory.GraphType;


public class BasicGraphTest {

    private Graph graph;
    
    @Before
    public void setUp() {
        this.graph = GraphFactory.create(GraphType.DIRECTED);
        
        assertNotNull(graph.getVertices());
        assertNotNull(graph.getEdges());
    }

    @After
    public void tearDown() {
        graph.clear();
    }
    
    @Test
    public void testCreateVertex() {
        Vertex vertex1 = graph.createVertex();
        
        assertNotNull(vertex1);
        assertEquals(1, graph.getVertices().size());
        for (Vertex vertex : graph.getVertices()) {
            assertEquals(vertex1, vertex);
        }
    }

    @Test
    public void testUniqueVertex() {
        Vertex vertex1 = graph.createVertex();
        Vertex vertex2 = graph.createVertex();
        
        assertNotNull(vertex1);
        assertNotNull(vertex2);
        assertFalse(vertex1.equals(vertex2));
    }

    @Test
    public void testUniqueEdge() {
        Vertex vertex1 = graph.createVertex();
        Vertex vertex2 = graph.createVertex();
        
        Edge edge1 = graph.createEdge(vertex1, vertex2);
        Edge edge2 = graph.createEdge(vertex1, vertex2);
        
        assertNotNull(edge1);
        assertNotNull(edge2);
        assertFalse(edge1.equals(edge2));
    }
    
    @Test
    public void testCreateEdge() {
        Vertex vertex1 = graph.createVertex();
        Vertex vertex2 = graph.createVertex();
        
        Edge edge1 = graph.createEdge(vertex1, vertex2);
        
        assertNotNull(edge1);
        assertEquals(vertex1, edge1.getSourceVertex());
        assertEquals(vertex2, edge1.getTargetVertex());
        
        assertEquals(1, graph.getEdges().size());
        for (Edge edge2 : graph.getEdges()) {
            assertEquals(edge1, edge2);
        }
    }

    @Test
    public void testProperty() {
        Vertex vertex1 = graph.createVertex();
        String key = "key";
        String value = "value";
        
        vertex1.setProperty(key, value);
        
        assertEquals(value, vertex1.getProperty(key));
        assertNotNull(vertex1.getPropertyKeys());
        assertTrue(vertex1.getPropertyKeys().contains(key));
    }

    @Test
    public void testElementLabel() {
        String label1 = "label1";
        String label2 = "label2";
        
        Vertex vertex1 = graph.createVertex(label1);
        Vertex vertex2 = graph.createVertex();
        
        Edge edge = graph.createEdge(vertex1, vertex2, label2);

        assertEquals(label1, vertex1.getLabel());
        assertEquals(label2, edge.getLabel());
    }
}
